<template>
  <a-card>
    <div class="headerBox">
      <a-button type="primary" @click="handleAdd">新增授权页面</a-button>
      <a-button type="primary" @click="handleAdd">批量删除</a-button>

      <a-button type="primary" @click="handleSubmit">确认</a-button>
    </div>
    <br />
    <a-table
      :columns="columns"
      :data-source="tableData"
      :loading="loading"
      :pagination="false"
      childrenColumnName="childList"
      @change="handleTableChange"
      defaultExpandAllRows
      :row-selection="{
        selectedRowKeys: selectedRowKeys,
        onChange: onSelectChange,
      }"
      :rowKey="
        (record, index) => {
          return record.id;
        }
      "
    >
      <template slot="action" slot-scope="text, record">
        <a @click="handleEdit(record)">编辑</a>
        <a-divider type="vertical" />
        <a-popconfirm
          okText="确定"
          cancelText="取消"
          v-if="tableData.length"
          title="确定删除吗?"
          @confirm="() => handleDelete(record)"
        >
          <a>删除</a>
        </a-popconfirm>
      </template>
    </a-table>
    <PermissionModal
      ref="modalForm"
      :treeData="tableData"
      @ok="modalFormOk"
      :selectedRowKeys="selectedRowKeys"
    />
  </a-card>
</template>

<script>
import { tableMinxins } from "@/unit/mixins/tableMinxins.js";
import PermissionModal from "./PermissionModal.vue";
import { permissionColumns } from "./tableConfig";
import { SwitchNumber, FindObjFromList } from "@/unit/tool";
import api from "@/api/apiServer";
export default {
  mixins: [tableMinxins],
  components: { PermissionModal },
  props: {
    userId: {
      type: Number,
      default: 0,
    },
    resources: {
      type: String,
    },
  },
  data() {
    return {
      columns: permissionColumns,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 获取列表数据
    async getList() {
      const res = await api.ResourcesTree();
      this.tableData = res.data;
    },
    setPermission(resources) {
      this.selectedRowKeys = SwitchNumber(resources);
    },
    modalFormOk(selectedRowKeys) {
      this.getList();
      this.selectedRowKeys = selectedRowKeys;
    },
    onSelectChange(selectedRowKeys, selectedKeys) {
      let haveParent = false;
      selectedKeys.forEach((el) => {
        if (el.parentId != 0 && selectedRowKeys.indexOf(el.parentId) == -1) {
          haveParent = true;
          selectedRowKeys.push(el.parentId);
          let obj = FindObjFromList(this.tableData, "id", el.parentId);
          if (Object.keys(obj).length) {
            selectedKeys.push(obj);
          }
        }
      });
      if (haveParent) {
        this.onSelectChange(selectedRowKeys, selectedKeys);
      }
      this.selectedRowKeys = selectedRowKeys;
    },
    async handleSubmit() {
      let resources = this.selectedRowKeys.toString();
      const params = {
        id: this.userId,
        resources,
      };
      const res = await api.RoleEdit(params);
      this.$message.success(res.message);
      this.$emit("ok");
      this.getList();
    },
    // 删除
    async handleDelete(record) {
      let params = {
        userName: record.userName,
        id: record.id,
      };
      const res = await api.ResourcesDelete(params);
      this.$message.success(res.message);
      this.getList();
    },
  },
};
</script>

<style lang="less" scoped>
button {
  margin-right: 13px;
}

.headerBox {
  padding: 0 20px 0 35px;
}
</style>
